/*
 * PConexion.java
 *
 * Created on 5 de septiembre de 2006, 08:48 AM
 */

package ig.formato.gui.app;


import ig.formato.modelo.MSGDB;
import ig.basic.util.IMetodosPantallas;
import ig.formato.basic.ManipulacionDatos;
import ig.formato.util.IGFormatoException;
import ig.formato.util.CodigoBotones;
import ig.formato.util.NinguDatoSeleccionadoException;
import ig.formato.util.OBJETOS;
import java.awt.Component;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;

/**
 *
 * @author  jsanchez
 */
public class PSGDB extends javax.swing.JPanel implements IMetodosPantallas{
    private ManipulacionDatos mpd;
    private MSGDB modelo=null;
    private JFrame frame=null;
    /** Creates new form PConexion */
    public PSGDB(JFrame frame) {
        initComponents();
        this.frame=frame;
        this.Puerto.setDocument(new ig.formato.util.ValueDocument("INTEGER",100000D));
        mpd = new ManipulacionDatos(true,false);
        mpd.setConfimacioSalir(true,"Desea cerrar la ventana?");
        mpd.setPanelMetodosPantalla(this);
        mpd.setSize(600,270);
        mpd.setResizable(false);
        mpd.setMaximizable(false);
        mpd.setTitle("Configuracion de conexiones");
        modelo= new MSGDB(this);
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabel1 = new javax.swing.JLabel();
        Nombre = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        Driver = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        URL = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        Puerto = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();

        jLabel1.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel1.setText("Nombre");

        Nombre.setFont(new java.awt.Font("Dialog", 0, 10));
        Nombre.setToolTipText("Nombre con el que identifica la conexi\u00f3n");
        Nombre.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel3.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel3.setText("Driver");

        Driver.setFont(new java.awt.Font("Dialog", 0, 10));
        Driver.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel4.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel4.setText("URL");

        URL.setFont(new java.awt.Font("Dialog", 0, 10));
        URL.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel6.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabel6.setText("Puerto");

        Puerto.setFont(new java.awt.Font("Dialog", 0, 10));
        Puerto.setDisabledTextColor(new java.awt.Color(0, 0, 0));

        jLabel2.setFont(new java.awt.Font("Dialog", 1, 14));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText(org.openide.util.NbBundle.getMessage(PSGDB.class, "PSGDB.jLabel2.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jLabel6, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel1))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(Nombre, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, Driver, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                            .add(layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(Puerto, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                                    .add(URL, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE))))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 33, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(Nombre, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(Driver, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(URL, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(Puerto, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    
    public JInternalFrame getInternalFrame() {
        return mpd;
    }
    
    public void validar() throws ig.formato.util.IGFormatoException {
        if(this.Nombre.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un nombre","Escriba el nombre del Sistema Gestor de Base de Datos.");
        
        if(this.Driver.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un Driver","Escriba el Driver para el Sistema Gestor de Base de Datos.");
        
        if(this.URL.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un URL","Escriba un URL para el Sistema Gestor de Base de Datos.");
        
        if(this.Puerto.getText().trim().equals(""))
            throw new ig.formato.util.IGFormatoException("No se escribio un puerto","Escriba el Puerto para el Sistema Gestor de Base de Datos.");
        
        ((ig.formato.dto.SgbdDTO)this.modelo.getTO()).setNombre(this.Nombre.getText().trim());
        ((ig.formato.dto.SgbdDTO)this.modelo.getTO()).setDriver(this.Driver.getText().trim());
        try{
            ((ig.formato.dto.SgbdDTO)this.modelo.getTO()).setPuerto(Integer.valueOf(this.Puerto.getText().trim()));
        }catch(NumberFormatException e){
            throw new ig.formato.util.IGFormatoException("No se escribio un puerto Numertico","Escriba el un puerto correcto.");
        }
        ((ig.formato.dto.SgbdDTO)this.modelo.getTO()).setUrl(this.URL.getText().trim());
    }
    
    public static void main(String[] arg){
        Integer.valueOf("ii");
    }
    public void guardar() throws ig.formato.util.IGFormatoException {
        modelo.guardar();
        actualizar();
    }
    
    public void buscar() throws ig.formato.util.IGFormatoException, ig.formato.util.NinguDatoSeleccionadoException{
        DBusqueda dialog=new DBusqueda(this.getFrame(),this.modelo,modelo.buscar());
        dialog.setVisible(true);
        if(dialog.getOpcion().equals(OBJETOS.NO_SELECCION)){
            throw new ig.formato.util.NinguDatoSeleccionadoException();
        }else{
            this.actualizar();
        }
    }
    
    public void modificar() throws ig.formato.util.IGFormatoException {
        modelo.modificar();
    }
    
    public void nuevo() throws ig.formato.util.IGFormatoException {
        modelo.nuevo();
    }
    
    public void eliminar() throws ig.formato.util.IGFormatoException {
        modelo.eliminar();
    }
    
    public void salir() throws ig.formato.util.IGFormatoException {
        modelo.salir();
    }
    
    public void imprimir() throws ig.formato.util.IGFormatoException {
        this.mpd.parametros.put("JASPER","SGBD");
    }
    public Map getParametros() throws IGFormatoException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public ig.basic.util.IModelo getModelo() throws IGFormatoException {
        return this.modelo;
    }    
    public void limpiar() throws IGFormatoException {
        
    }
    
    public void cancelar() throws ig.formato.util.IGFormatoException {
        modelo.cancelar();
//        modelo.buscar();
        this.actualizar();
    }
    
    public Component[] getControles() {
        return this.getComponents();
    }
    
    public void setFrame(JFrame frame) {
        this.frame=frame;
    }
    
    public JFrame getFrame() {
        return frame;
    }
    
    
    public Object getTO() throws ig.formato.util.IGFormatoException {
        return modelo.getTO();
    }
    
    public void actualizar() throws IGFormatoException {
        if(((ig.formato.dto.SgbdDTO)modelo.getTO()).getId()!=null){
            this.Nombre.setText(((ig.formato.dto.SgbdDTO)modelo.getTO()).getNombre());
            this.Driver.setText(((ig.formato.dto.SgbdDTO)modelo.getTO()).getDriver());
            this.URL.setText(((ig.formato.dto.SgbdDTO)modelo.getTO()).getUrl());
            this.Puerto.setText(((ig.formato.dto.SgbdDTO)modelo.getTO()).getPuerto().toString());
        }
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Driver;
    private javax.swing.JTextField Nombre;
    private javax.swing.JTextField Puerto;
    private javax.swing.JTextField URL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
    
}
